﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Globalization;
using System.IO;

namespace AlarmClock
{
    public partial class FrmDisplayCountDown : Form
    {
        private String infoText;
        private DateTime countdownDate;
        private int currentIndex;
        private int maxIndex;
        private DataBase database;
        private CountDown nowCountDown;
        private Image[] btnBack = new Image[] { AlarmClock.Properties.Resources.btn_last, AlarmClock.Properties.Resources.btn_last_down, AlarmClock.Properties.Resources.btn_next, AlarmClock.Properties.Resources.btn_next_down };

        //无边窗体的拖动
        [DllImport("user32.dll")]  //需添加using System.Runtime.InteropServices
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        public FrmDisplayCountDown()
        {
            InitializeComponent();
        }
        public FrmDisplayCountDown(int index, DataBase db)
        {
            InitializeComponent();
            nowCountDown = db.getCountDown(index);
            infoText = nowCountDown.lable;
            countdownDate = nowCountDown.time;
            currentIndex = index;
            maxIndex = db.getCount(3) - 1;
            database = db;
        }
        private void FrmDisplayCountDown_Load(object sender, EventArgs e)
        {
            timCountDown_Tick(sender, e);
            timeNow_Tick(sender, e);
            LlblInfo.setText(nowCountDown.lable);
            if (maxIndex == 0)
            {
                btnLast.Enabled = false;
                btnNext.Enabled = false;
                return;
            }
            if (currentIndex == 0) btnLast.Enabled = false;
            else if (currentIndex == maxIndex) btnNext.Enabled = false;
        }

        private void Frm_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        private void LlblInfo_MouseHover(object sender, EventArgs e)
        {
            LlblInfo.label_MouseHover(sender, e);
        }

        private void btnLast_MouseHover(object sender, EventArgs e)
        {
            btnLast.BackgroundImage = btnBack[0];
        }

        private void btnLast_MouseLeave(object sender, EventArgs e)
        {
            btnLast.BackgroundImage = null;
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            btnLast.BackgroundImage = btnBack[1];
            currentIndex --;
            onCountDownChanged();
        }

        private void btnNext_MouseHover(object sender, EventArgs e)
        {
            btnNext.BackgroundImage = btnBack[2];
        }

        private void btnNext_MouseLeave(object sender, EventArgs e)
        {
            btnNext.BackgroundImage = null;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            btnNext.BackgroundImage = btnBack[3];
            currentIndex++;
            onCountDownChanged();
        }

        private void timCountDown_Tick(object sender, EventArgs e)
        {
            DateTime nowTime = DateTime.Now;
            if (nowTime < countdownDate)
            {
                int[] times = CalculatorInt.CalculateDeltaTime(nowTime, countdownDate);
                int totalSeconds = (int)((nowCountDown.time - nowTime).TotalSeconds);
                int length = times.Length;
                switch (times.Length)
                {
                    case 5:
                        lblDeltaDay.Text = times[0] + "年" + times[1] + "天";
                        lblDeltaTime.Text = times[2] + "时" + times[3] + "分" + times[4] + "秒";
                        break;
                    case 4:
                        lblDeltaDay.Text = times[0] + "天";
                        lblDeltaTime.Text = times[1] + "时" + times[2] + "分" + times[3] + "秒";
                        break;
                    case 3:
                        lblDeltaTime.Text = times[0] + "时" + times[1] + "分" + times[2] + "秒";
                        break;
                    case 2:
                        lblDeltaTime.Text = times[0] + "分" + times[1] + "秒";
                        break;
                    case 1:
                        lblDeltaTime.Text = times[0] + "秒";
                        break;
                }
                lblTotalSecond.Text = lblTotalSecond.Tag.ToString().Replace("s", totalSeconds + "");
                if (length > 3)
                {
                    lblDeltaDay.Visible = true;
                    lblTotalSecond.Visible = true;
                }
                else if (length > 1)
                {
                    lblDeltaDay.Visible = false;
                    lblTotalSecond.Visible = true;
                }
                else if (length == 1)
                {
                    lblDeltaDay.Visible = false;
                    lblTotalSecond.Visible = false;
                    if (totalSeconds <1)
                    {
                        TimeUp.Ring(RingType.RING_LONG);
                        lblDeltaTime.Text = "已结束";
                        timCountDown.Stop();
                        timCountDown.Enabled = false;
                    }
                }
                lbl1.Visible = lbl2.Visible = true;
            }
            else
            {
                lblDeltaDay.Visible = false;
                lblTotalSecond.Visible = false;
                lblDeltaTime.Text ="已结束";
                timCountDown.Stop();
                timCountDown.Enabled = false;
                lbl1.Visible = lbl2.Visible = false;
            }
        }

        private void timeNow_Tick(object sender, EventArgs e)
        {
            DateTime nowTime = DateTime.Now;
            lblNowTime.Text = nowTime.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
        }

        private void onCountDownChanged()
        {
            nowCountDown = database.getCountDown(currentIndex);
            LlblInfo.setText(nowCountDown.lable);
            countdownDate = nowCountDown.time;
            timCountDown.Enabled = true;
            timCountDown.Start();
            if (maxIndex > 0)
            {
                if (currentIndex == maxIndex)
                    btnNext.Enabled = false;
                else
                    btnNext.Enabled = true;
                if (currentIndex == 0)
                    btnLast.Enabled = false;
                else
                    btnLast.Enabled = true;
            }
        }

        private void tsmClose_Click(object sender, EventArgs e)
        {
            TimeUp.Stop();
            this.Close();
        }
    }
}
